//
//  NSDictionary+Log.m
//  url使用
//
//  Created by stone on 16/4/22.
//  Copyright (c) 2016年 stone. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale {
    
    NSMutableString * string = [NSMutableString string];
    
    // 开头有个{
    [string appendString:@"{\n"];
    
    // 遍历所有的键值对
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL * stop) {
        
        [string appendFormat:@"\t\"%@\"", key];
        
        [string appendString:@": "];
        
        if ([obj isKindOfClass:[NSString class]]) {
            
            [string appendFormat:@"\"%@\",\n", obj];
        }else{
            [string appendFormat:@"%@,\n", obj];
        }
    }];
    
    // 结尾有个}
    [string appendString:@"}"];
    
    // 查找最后一个逗号
    /*
     将rangeOfString:发送给一个NSString对象时，传递的参数时要查找的字符串。它会返回一个NSRange struct来告诉你一个与这个字符串相匹配的部分从哪里开始以及匹配上的字符个数。
     */
    NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
    
    if (range.location != NSNotFound) {
        [string deleteCharactersInRange:range];
    }
    
    return string;
}

@end

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale {
    NSMutableString * string = [NSMutableString string];
    
    // 开头有个(
    [string appendString:@"[\n"];
    
    // 遍历所有的元素
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
        
        [string appendFormat:@"\t%@,\n", obj];
    }];
    
    // 结尾有个)
    [string appendString:@"]"];
    
    // 查找最后一个逗号
    NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
    if (range.location != NSNotFound)
        [string deleteCharactersInRange:range];
    
    return string;
}
@end
